<?php
/**
*
* 功能描述（接口控制器）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class CtrlApi extends CtrlBase
{
	public $paramCheck = array(
		'zfbreturn' => array(
			'succ' => array('GET', 'must', 'intval', 'bool'),
			'id' => array('GET', 'must', 'trim', 'notempty'),
			'oid' => array('GET', 'must', 'trim', 'notempty'),
			'status' => array('GET', 'must', 'trim', 'notempty'),
		),
	);
	public $perm = array('allow', '');

	//这个API仅用来显示
	function ZfbReturn()
	{
		if($this->params['succ'] == 0)
			return RT_ATTACK;
		$id = GetDataId($this->params['id']);
		$obj = $this->GetModel('trade');
		$data = $obj->GetItem($id);
		if(!$data)
			return RT_ATTACK;
		$newSta = $this->params['status'];		//支付宝返回状态
    	$succ = ($newSta == 'TRADE_FINISHED' || $newSta == 'TRADE_SUCCESS');
		
		switch(intval($data['trade_type']))
		{
		case 1: $msg = $this->GetChargeMsg($succ, $data); break;
		case 2: $msg = $this->GetPayOrderMsg($succ, $data); break;
		default:
			return RT_ATTACK;
		}
		$this->assign("msg", $msg);
		return RT_MSG;
	}

	private function GetPayOrderMsg($succ, &$data)
	{
		$oid = $data['id'];
		$vid = ORDERBASE + $oid;
		if($succ)
		{
			switch($data['status'])
			{
			case 1:
				$msg = "付款还在进行中，请您耐心等待短信通知";
				break;
			case 2:
				$msg = "付款成功";
				break;
			case 3:
				$msg = "付款失败";
			case 4:
				$msg = "付款失败, 原因:".$data['info'];
				break;
			default:
				$msg = "数据异常";
			}
		}
		else
			$msg = "付款失败";
		$msg = $vid.$msg."<br/><a href='index.php?action=order.detail&id=$oid'>点击查看订单$vid</a>";
		return $msg;
	}

	private function GetChargeMsg($succ, &$data)
	{
		if($succ)
		{
			switch($data['status'])
			{
			case 1:
				$msg = "付款还在进行中，请您耐心等待短信通知";
				break;
			case 2:
				$msg = "消费".$data['amount']."充值《".$data['info']."》成功";
				break;
			case 3:
				$msg = "付款失败";
			case 4:
				$msg = "充值失败, 原因:".$data['info'];
				break;
			default:
				$msg = "数据异常";
			}
		}
		else
			$msg = "付款失败";
		$msg .= "<br/><a href='index.php?action=user.index'>点击查看余额</a>";
		return $msg;
	}
}

